<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Base
 *
 * @author dxx
 */

namespace app\common\controller;

use app\common\model\log\soa2Logs as soa2LogsModel;
use app\common\model\oms\Companies as CompaniesModel;
use app\common\common\Helper;

class Base extends \think\Controller {
    
    use \app\common\traits\ApiPost;

    protected $_apiUserInfo;

    public function __construct(\think\Request $request = null) {
        parent::__construct($request);
        
    }

    protected function input() {
        $contents = file_get_contents("php://input");
        $param = json_decode($contents, TRUE);
        if (empty($param))
            return $this->output([], 'json error', '0004');
        return $param;
    }


//    protected function output($params = [], $msg = 'success', $code = '200') {
//        $inPut = file_get_contents("php://input");
//        return Helper::output($params,$msg,$code,$inPut,$this->_apiUserInfo);
//    }

//    protected function outPutError($params) {
//        $inPut = file_get_contents("php://input");
//        return Helper::outPutError($params, $inPut, $this->_apiUserInfo);
//    }


    protected function addLog($outPut) {
        $soa2LogsModel = new soa2LogsModel();
        $soa2LogsModel->addLog(file_get_contents("php://input"), $outPut, $this->_apiUserInfo);
    }

    
    

    /**
     * 接口验证参数的方法
     * @param $param_list
     * @param $params
     * @return bool
     */
    protected function diffParam($param_list, $params) {
        $must_params = array_filter($param_list);
        $diff_array = array_diff_key($must_params, $params);
        if (!empty($diff_array)) {
            $warn = key($diff_array);
            $this->outPutError(['msg' => "$warn don't find "]);
        }

        //$format上面的0、不必须，1、必须有string;2、必须有number;3、必须有数组
        foreach ($param_list as $key => $format) {
            switch ($format) {
                case 'string':
                    if (empty($params[$key])) {
                        $this->outPutError(['msg' => "$key format is empty"]);
                    }
                    break;
                case 'number':
                    if (empty($params[$key]) || !is_numeric($params[$key])) {
                        $this->outPutError(['msg' => "$key format must number"]);
                    }
                    break;
                case 'array':
                    if (empty($params[$key]) || !is_array($params[$key])) {
                        $this->outPutError(['msg' => "$key format must array"]);
                    }
                    break;
                case 'mobile':
                    if (empty($params[$key]) || !preg_match('/^1[34578]{1}\d{9}$/', $params[$key])) {
                        $this->outPutError(['msg' => "$key format is error"]);
                    }
                case 'int':
                    $params[$key] = intval($params[$key]);
                    break;
                case 'date':
                    if ($params[$key] != date('Y-m-d', strtotime($params[$key]))) {
                        $this->outPutError(['msg' => "$key format is error"]);
                    }
                    break;
                default:
                    break;
            }
        }

        return true;
    }

    /**
     * 根据companyCode获取companyId
     */

    protected function  getCompanyIdByCompanyCode($companyCode) {
        //判断CompanyCode是否正确

        $companiesModel = new CompaniesModel();
        $companyInfo = $companiesModel->getCompanyInfoByCondition("company_id", ['company_code' => $companyCode], true);
        $company_id = $companyInfo['company_id'];
        
        if (!$company_id) {
            return $this->outPutError(['msg' => 'companyCode is error']);
        }

        return $company_id;
    }

    /**
     * 转化模型成为arr类型
     * @param type $data
     * @return type
     */
    protected function TransModelArr($data){
        return json_decode(json_encode($data),true);
    }
    
     /**
     * 根据companyId获取companyCode
      * @param $companyId type int 集团ID
      * @return $companyCode type string 集团名称(中文或字母)
     */
    protected function getCompanyCodeByCompanyId($companyId) {
        //判断CompanyId是否正确
        $companiesModel = new CompaniesModel();
        $companyInfo = $companiesModel->getCompanyInfoByCondition("company_code", ['company_id' => $companyId], true);
        $company_code = $companyInfo['company_code'];
        if (!$company_code) {
            return $this->outPutError(['msg' => 'companyId is error']);
        }
        
        return $company_code;
    }
}
